package com.craftplay.service;


import com.alibaba.fastjson.JSONObject;
import com.craftplay.common.PageBean;
import com.craftplay.dao.UnitMapper;
import com.craftplay.model.*;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MomentService {

    @Autowired
    private UnitMapper unitMapper;


    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;


    @Autowired
    private BehaveService behaveService;

    public int addMoment(Unit unit) {
        return unitMapper.insert(unit);
    }

    public Unit oneMomentDetail(int unitId) {
        return unitMapper.selectByPrimaryKey(unitId);
    }

    public void addViewNum(int unitId) {
        unitMapper.addViewNum(unitId);
    }

    public List<Unit> findAllMoment() {
        return unitMapper.findAll();
    }

    public List<Unit> findMomentByCondition(String address, int kind, String scope, double latitude, double longitude) {
        return unitMapper.selectUnit(address, kind, scope, latitude, longitude);
    }

    public List<Unit> findMomentByUser(int userId) {
        return unitMapper.selectByUser(userId);
    }

    public int updateMoment(Unit unit) {
        return unitMapper.updateByPrimaryKeySelective(unit);
    }

    public int updateByPageMoment(Unit unit) {
        return unitMapper.updateByPage(unit);
    }


    public List<Unit> getUnitList(Unit unit) {
        return unitMapper.getUnitList(unit);
    }

    public PageBean<Unit> findAllByKind(int pageNum, int pageSize, Integer kind, String scope, String address) {
        PageHelper.startPage(pageNum, pageSize);
        List<Unit> allByFocusUserId = unitMapper.findAllByKind(kind, scope, address);
        return new PageBean<>(allByFocusUserId);
    }


    //删除活动，同时删除评论
    public int delUnit(int unitId) {
        //删除评论
        List<Comment> comments = commentService.selectCommentByParentId((long) unitId);
        for (Comment com : comments) {
            List<Comment> subCommentList = commentService.selectCommentByParentId(com.getId());
            for (Comment comSub : subCommentList) {
                commentService.deleteCommentByCommentId(comSub.getId());
            }
            commentService.deleteCommentByCommentId(com.getId());
        }
        //删除活动
        return unitMapper.deleteByPrimaryKey(unitId);
    }

    //获得所有回复
    public List<SkillsGive> getUnitReplyList(int unitId) {
        List<Unit> unitReplyList = unitMapper.getUnitReplyList(unitId);
        List<SkillsGive> resultList = new ArrayList<>();
        unitReplyList.forEach(r -> {
            SkillsGive replay = new SkillsGive();
            BeanUtils.copyProperties(r, replay);
            //是否匿名
            if (r.getAnonymous() == 0) {
                User user = userService.selectUserById(r.getUserId());
                replay.setHeadImgUrl(user.getHeadUrl());
                replay.setUserName(user.getNickname());
            } else {
                replay.setHeadImgUrl("http://img.playpro.live/20190709223755.png");
                replay.setUserName("匿名匠友");
            }
            resultList.add(replay);
        });
        return resultList;
    }

    //获得所有指点
    public List<Unit> getUnitDirectionsList(int unitId) {
        List<Unit> unitReplyList = unitMapper.getUnitDirectionsList(unitId);
        return unitReplyList;
    }

    //获得用户的所有指点
    public List<Unit> getUnitByUserId(Unit unit) {
        List<Unit> unitReplyList = unitMapper.getUnitByUserId(unit);
        return unitReplyList;
    }

    public PageBean<Unit> findAllByFocusUserId(int pageNum, int pageSize, List<Integer> userId, String scope, String address) {
        PageHelper.startPage(pageNum, pageSize);
        List<Unit> allByFocusUserId = new ArrayList<>();
        if (userId.size() > 0) {
            allByFocusUserId = unitMapper.findAllByFocusUserId(userId, scope, address);
        }
        return new PageBean<>(allByFocusUserId);
    }


    //获取list中的活动
    public List<Unit> findAllByUnitList(List<Integer> unitList) {
        return unitMapper.findAllByUnitList(unitList);
    }

    /**
     * 所有自己参与的回答、提问、动态
     *
     * @param pageNum
     * @param pageSize
     * @param kindList
     * @param userId
     * @param scope
     * @param address
     */
    public PageBean<Unit> findAllByMyKind(int pageNum, int pageSize, List<Integer> kindList, Integer userId, String scope, String address, Boolean anonymous) {
        PageHelper.startPage(pageNum, pageSize);
        List<Unit> allByFocusUserId = unitMapper.findAllByMyKind(kindList, userId, scope, address, anonymous);
        return new PageBean<>(allByFocusUserId);
    }

    /**
     * 获得未读消息数量
     *
     * @param userId
     */
    public int getUnReadCount(Integer userId) {
        return behaveService.getMyUnReadCount(userId);
    }

    /**
     * 我的消息
     *
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    public PageBean<BehaveModel> getMyMessage(int pageNum, int pageSize, Integer userId, Integer isRead) {
        return behaveService.getBehaveList(pageNum, pageSize, userId, isRead);
    }

    /**
     * 查询课程
     *
     * @param scope
     * @param address
     * @return
     */
    public PageBean<Unit> findAllByClassGroupByUserId(String scope, String address) {
        List<Unit> userList = unitMapper.findAllByClassGroupByUserId(scope, address);
        return new PageBean<>(userList);
    }

    /**
     * 查询用户课程
     *
     * @param userId
     * @param scope
     * @param address
     * @param limit
     * @return
     */
    public List<Unit> findClassByUserId(Integer userId, String scope, String address, int limit) {
        List<Unit> userList = unitMapper.findClassByUserId(userId, scope, address, limit);
        return userList;

    }
}
